<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>\
    <link rel="stylesheet" href="../刘佳富/Css/reset.css">
    <style>
        .box {
            background-color: brown;
            width: 200px;
            height: 300px;
            margin: 100px auto;
            display: flex;
            flex-direction: column;
            align-items: center;
            color: white;
        }

        .div1 {
            height: 80px;
            line-height: 80px;
            font-size: 14px;
        }

        .div2 {
            height: 80px;
            line-height: 80px;
            font-size: 18px;
            display: flex;
            align-items: center;
            text-align: center;
        }

        .box>h2 {
            font-size: 25px;
            font-weight: normal;
        }

        .div2>span {
            background-color: rgb(41, 40, 40);
            height: 30px;
            width: 30px;
            line-height: 30px;
            margin: 0 5px;
        }

        .div3 {
            flex: 1;
            text-align: center;
            align-content: end;
            padding-bottom: 25px;
            font-size: 18px;
        }
    </style>
</head>

<body>
    <div class="box">
        <div class="div1">
            <p>今天是<span class="spans">2024年8月08日</span></p>
        </div>
        <h2>下班倒计时</h2>
        <div class="div2">
            <span class="hour">23</span>:<span class="minnutes">30</span>:<span class="scond">31</span>
        </div>
        <div class="div3">
            <p class="shijian"><span class="ds">18:30:00</span>下课</p>
        </div>
    </div>

    <script>
        function colorBk() {
            let r = Math.floor(Math.random() * 255)
            let g = Math.floor(Math.random() * 255)
            let b = Math.floor(Math.random() * 255)
            return `rgb(${r},${g},${b})`
        }
        // getCountTime()
        // 开启定时器
        function getCountTime() {
            const box = document.querySelector('.box')
            box.style.backgroundColor = colorBk()
            const hour = document.querySelector('.hour')
            const minnutes = document.querySelector('.minnutes')
            const scond = document.querySelector('.scond')
            //得到当前时间戳
            const now = +new Date()
            //得到将来的时间戳
            const last = +new Date('2024-08-17 15:50:00')

            // console.log(now, last);
            const count = (last - now) / 1000;
            // 将来时间减去现在时间 毫秒 总的毫秒
            // console.log(count);
            // let h = parseInt(总秒数 / 60 / 60 % 24) 小时
            // let m = parseInt(总秒数 / 60 % 60) 分钟
            // let s = parseInt(总秒数 % 60) 秒数
            let h = parseInt(count / 60 / 60 % 24)
            h = h < 10 ? '0' + h : h
            let m = parseInt(count / 60 % 60)
            m = m < 10 ? '0' + m : m
            let s = parseInt(count % 60)
            s = s < 10 ? '0' + s : s
            // console.log(h, m, s)
            hour.innerHTML = h
            minnutes.innerHTML = m
            scond.innerHTML = s
            // 停止定时器
            if (h == '00' && m == '00' && s == '00') {
                clearInterval(setTime)
            }
        }
        getCountTime()
        let setTime = setInterval(getCountTime, 1000)

        // ai优化

        // function colorBk() {
        //     const r = Math.floor(Math.random() * 255);
        //     const g = Math.floor(Math.random() * 255);
        //     const b = Math.floor(Math.random() * 255);
        //     return `rgb(${r},${g},${b})`;
        // }

        // function updateTime() {
        //     const now = new Date();
        //     const last = new Date('2024-08-02 23:18:00');
        //     const count = (last - now) / 1000;

        //     const h = Math.floor(count / 3600).toString().padStart(2, '0');
        //     const m = Math.floor((count % 3600) / 60).toString().padStart(2, '0');
        //     const s = Math.floor(count % 60).toString().padStart(2, '0');

        //     document.querySelector('.hour').innerHTML = h;
        //     document.querySelector('.minnutes').innerHTML = m;
        //     document.querySelector('.scond').innerHTML = s;
        //     document.querySelector('.box').style.backgroundColor = colorBk();

        //     if (h == '00' && m == '00' && s == '01') {
        //         clearInterval(setTime); // 停止动画
        //         document.querySelector('.box').style.backgroundColor = '#000'; // 倒计时结束的背景色
        //     }
        // }


        // let setTime = setInterval(updateTime, 1000); // 开始倒计时


        // updateTime();

    </script>
</body>

</html>